import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable, Inject } from '@angular/core';

import { Observable } from 'rxjs/Observable';

import * as moment from 'moment';
import { Warn } from '../models';

import 'rxjs/add/operator/map'
import 'rxjs/add/operator/catch'

@Injectable()
export class WarnService {
  // offset = 0
  // length = 5;

  apiUrl = 'http://localhost:3000/warn/'

  constructor(private http: HttpClient, @Inject('API_URL') private url: string) {
  }

  //所有
  queryAll(offset:number,length:number): Observable<any[]> {
    
    const url = `${this.url}warning/essential?offset=${offset}&length=${length}`;
    console.log(url);
    return this.http
      .get<any[]>(url)
      .map(data => {                      // 返回 数据
        const warns: Warn[] = [];          //  定义空数组
        data.forEach(item => {         // 对请求数据进行遍历
          warns.push(                   // 将格式化的数据放到新数组中
            Object.assign(item, {
              //  YYYYMMDDHHmmss格式转换为YYYY-MM-DD hh:mm:ss  格式化
              time: moment(item.time, 'YYYYMMDDHHmmss').format('YYYY-MM-DD hh:mm:ss')
            }));
        });
        return warns;
      });
  }


  //读取全部告警
  allWarn(warn: Warn,offset:number,length:number): Observable<Object> {
    const url = `${this.url}warnings/essential/criteria?offset=${offset}&length=${length}`;
    console.log(url);
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json;charset=UTF-8');
    return this.http
      .post(url, JSON.stringify(warn), { headers: headers });
  }



  //已处理
  Resolved(offset:number,length:number): Observable<any[]> {
    const url = `${this.url}warning/essential/warn/1?offset=${offset}&length=${length}`;
    console.log(url);
    return this.http
      .get<any[]>(url)
      .map(data => {                      // 返回 数据
        const warns: Warn[] = [];          //  定义空数组
        data.forEach(item => {         // 对请求数据进行遍历
          warns.push(                   // 将格式化的数据放到新数组中
            Object.assign(item, {
              //  YYYYMMDDHHmmss格式转换为YYYY-MM-DD hh:mm:ss  格式化
              time: moment(item.time, 'YYYYMMDDHHmmss').format('YYYY-MM-DD hh:mm:ss')
            }));
        });
        return warns;
      });
  }


  // 未处理
  Untreated(offset:number,length:number): Observable<any[]> {
    const url = `${this.url}warning/essential/warn/0?offset=${offset}&length=${length}`;
    console.log(url);
    return this.http
      .get<any[]>(url)
      .map(data => {                      // 返回 数据
        const warns: Warn[] = [];          //  定义空数组
        data.forEach(item => {         // 对请求数据进行遍历
          warns.push(                   // 将格式化的数据放到新数组中
            Object.assign(item, {
              // YYYYMMDDHHmmss格式转换为YYYY-MM-DD hh:mm:ss  格式化
              time: moment(item.time, 'YYYYMMDDHHmmss').format('YYYY-MM-DD hh:mm:ss')
            }));
        });
        return warns;
      });
  }

  //id打开详情
  queryById(warnId:string): Observable<Warn> {

    const url = `${this.url}warnings/disposition/${warnId}`;
    return this.http.get<Warn>(url)
    .map(body => {
        const warn = body;
        Object.assign(body, {
          time: moment(body.time, 'YYYYMMDDHHmmssSSS').format('YYYY-MM-DD hh:mm:ss')
        })
        return warn;
      })
  }

//读取意见
  queryByIdRemarks(warnId:string): Observable<Warn[]> {
        const url = `${this.url}warnings/disposition/${warnId}`;
        return this.http.get<Warn[]>(url);
      }


  //下拉刷新
  getDeviceList(startPage,len): Observable<any[]> {
    const url = `${this.url}`
    let params = new HttpParams()
      .set('offset',startPage)
      .set('length',len)
    return this.http.get<any>(url,{params:params})
  }
  

  //更新提交
  update(warn: Warn,warnId,userId): Observable<Object> {
    const url = `${this.url}warnings/disposition/${warnId}/${userId}`;
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json;charset=UTF-8');
    return this.http
      .post(url, JSON.stringify(warn), { headers: headers });
  }




  searchList(type, levelList, start, end): Observable<any> {
    const url = `${this.url}warn/data/queryWarnBySensorTypeAndWarnLevelsOfTimeRange`
    let params = new HttpParams()
      .set('sensorType', type)
      .set('start', start)
      .set('end', end);
      // .set('warnLevels',levelList)
    levelList.forEach(item =>
      params = params.append('warnLevels', `${item}`));
    return this.http.get(url, { params: params });
  }

}
